%% ���ļ���Ҫ���������ߵ�
clc;
clear;
close all;

%% ������ cpu ���� gpu�Ļ�����
tic;
mycaffe.reset_all();
caffe.set_mode_cpu();
% caffe.set_mode_gpu();
% caffe.set_device(0);

%% ������и����ļ�·��������
%caffe.version ���ĺ�����ܿ��Բ鿴caffe/matlab����Ľ���

net_solver = fullfile(mycaffe.modelpath, 'LightenedCNN', 'LCNN_solver.prototxt');
%net_model = './examples/SRCNN/SRCNN_net.prototxt';
%net_weight = './examples/SRCNN/caffemodel/XX.caffemodel';

%% �����ǡ�solver���в��������,refer to solver.prototxt is better
max_iter = 10000; %���ĵ�����
test_iter = 23;  %�������batch_size ����ÿ��epoch��Ӧ��ѵ�������ǵ�Ȼϣ���串��ѵ����
test_interval = 50; %�����ʾ���Եļ��
disp = 50 ;%ѵ��ʱ��ÿ���ٴ��ռ�һ�����,�����������test_interval
test_nums = 357;

%% ѵ�����Ͳ������
train_loss = zeros(1,floor(max_iter/disp));
test_loss = zeros(1,floor(max_iter/test_interval));

%% ������� traloss testloss
trainloss = 0;
testloss = 0;

%% ���濪ʼ�������磬���ҽ���һ��ǰ�򴫲�
solver = caffe.Solver(net_solver);

%net_caffe.Net(model,'test');
%net.copy_from(weight); %finetuning

for iter = 1:max_iter
     solver.step(1); %��ʼһ�δεĵ������1
 %   trainloss = trainloss+solver.net.blobs('loss').get_data()  %������Ľ����ǰ�ÿһ�ε��Ľ���ӡ����
     trainloss = trainloss + solver.net.blobs('loss').get_data();  %������Ľ����ǰ�ÿһ�ε��Ľ���ӡ����
    %������Ǽ�����ɴ�ȡƽ��
     if rem(iter,disp) == 0
        train_loss(floor(iter/disp)) = trainloss/disp;
        trainloss = 0;
     end
    
     if rem(iter,test_interval)  == 0  %���˵���ʱ��
        for  itertest = 1:test_iter   %��Զ��batch��ѵ����
            testloss = testloss+solver.net.blobs('loss').get_data();            
        end
        test_loss(floor(iter/test_interval)) = testloss/test_nums;
        testloss = 0;
     end  
end

%������ǽ��л�ͼ
figure;plot(train_loss,'r');hold on;plot(test_loss,'g');xlabel('iteration');ylabel('loss');title('our srcnn -100 result');legend('train_loss','test_loss')

toc;
































